package com.rwind.dynamic.service;

import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.rwind.dynamic.common.MessageDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.GenericWebApplicationContext;

import java.io.File;


@RestController
@RequestMapping("dynamicRegistTestController")
public class DynamicRegistTestController {
    private static final Logger logger = LoggerFactory.getLogger(DynamicRegistTestController.class);

    @Autowired
    private GenericWebApplicationContext context;
    /**
     * 模拟从远程下载准备要注册Bean的jar文件
     *
     * @return jar文件
     */
    private File getRmoteJarFile() {
        return new File("E:/IDEA_HOME/dynamic-regist-bean/dynamic-regist-bean/target/dynamic-regist-bean.jar");
    }

    @GetMapping("/test")
    public void test(){

    }

    @GetMapping("registBean")
    public MessageDto<?> registBean() {
        File jarFile = getRmoteJarFile();
        // 准备要注册的Bean类名
        String className = "org.example.service.impl.DynamicRegistServiceImpl";
        // 准备要注册的Bean名称
        String beanName = "dynamicRegistServiceImpl";
        // 注册完成后调用Bean的测试方法
        String invokeMethod = "serviceDo";
        Class<?> targetClass = ClassLoaderUtil.loadClass(jarFile, className);
        logger.info("本次要注册的bean className = {}", targetClass.getName());

        DynamicRegistUtil.registerBeanDefinition(beanName, targetClass);
        Object object = SpringUtil.getBean(beanName);
        ReflectUtil.invoke(object, invokeMethod);
        return MessageDto.getSuccess(null);
    }

    @GetMapping("unRegistBean")
    public MessageDto<?> unRegistBean() {
        String beanName = "dynamicRegistServiceImpl";
        logger.info("本次要卸载的bean beanName = {}", beanName);

        DynamicRegistUtil.unRegisterBeanDefinition(beanName);
        logger.info("卸载结果：{}", SpringUtil.getApplicationContext().containsBean(beanName) ? "失败" : "成功");
        return MessageDto.getSuccess(null) ;
    }

    @GetMapping("registController")
    public MessageDto<?> registController() throws Exception {
        File jarFile = getRmoteJarFile();
        // 准备要注册的Bean类名
        String className = "org.example.controller.DynamicRegistController";
        // 准备要注册的Bean名称
        String beanName = "dynamicRegistController";
        Class<?> targetClass = ClassLoaderUtil.loadClass(jarFile, className);
        logger.info("本次要注册的controller className = {}", targetClass.getName());

        DynamicRegistUtil.registerBeanDefinition(beanName, targetClass);
        DynamicRegistUtil.registerController(beanName);
        return MessageDto.getSuccess(null) ;
    }

    @GetMapping("unRegistController")
    public MessageDto<?> unRegistController() {
        String beanName = "dynamicRegistController";
        logger.info("本次要卸载的Controller beanName = {}", beanName);

        DynamicRegistUtil.unregisterController(beanName);
        DynamicRegistUtil.unRegisterBeanDefinition(beanName);
        logger.info("卸载结果：{}", SpringUtil.getApplicationContext().containsBean(beanName) ? "失败" : "成功");
        return MessageDto.getSuccess(null) ;
    }
}